import LoginBox from '../components/login-box/index.vue';

let loginBoxInstance;
let eventList = []


export default {
    install(Vue) {
        Vue.prototype.$loginBox = {
            show(mode) {
                if (!loginBoxInstance) {
                    const LoginBoxComponent = Vue.extend(LoginBox);
                    loginBoxInstance = new LoginBoxComponent({
                        el: document.createElement('div')
                    });
                    document.body.appendChild(loginBoxInstance.$el);
                }
                loginBoxInstance.show(mode);
                loginBoxInstance.init(eventList);

            },
            close() {
                if (loginBoxInstance) {
                    loginBoxInstance.close();
                    loginBoxInstance.$destroy();
                    document.body.removeChild(loginBoxInstance.$el);
                    loginBoxInstance = null;
                }
            },

            addEventListener(handler) {

                if (!eventList.includes(handler)) {
                    if (eventList.length > 0) {
                        eventList.push(handler);
                    } else {
                        eventList = [handler];
                    }
                }

            }
        };
    },
};